<template>
    <div>
        <div class="wrapper">
            <div
                style="margin: 200px auto; background-color: #ede6e6;opacity: 0.8; width: 350px; height: 380px; padding: 20px; border-radius: 10px">
                <div style="margin-top: 20px; text-align: center; font-size: 15px;padding: 0;"><b>欢迎来到宠物医院管理系统</b></div>
                <div style="margin-top: 20px; text-align: center; font-size: 24px"><b>登 录</b></div>
                <el-form :model="user" :rules="rules" ref="userForm">
                    <el-form-item prop="username">
                        <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user"
                            v-model="user.username"></el-input>
                        <!-- v-model 数据绑定 -->
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
                            v-model="user.password"></el-input>
                    </el-form-item>
                    <el-form-item label="">
                        <el-input size="medium" v-model="user.code" placeholder="验证码"
                            style="width: 200px;margin-right: 10px;"></el-input>
                        <img style="overflow: hidden;height: 36px;" :src="code" @click="getCheckCode" />
                    </el-form-item>
                    <el-form-item style="margin: 10px 0; text-align: center">
                        <el-button type="warning" size="small" autocomplete="off" @click="jumpRegister">注册</el-button>
                        <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>


                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { setRoutes } from "@/router";

export default {
    name: "Login",
    data() {
        return {
            user: {},
            rules: {
                username: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                    { min: 3, max: 11, message: '用户名长度在 3 到 11 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                    { min: 4, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '验证码不能为空', trigger: 'blur' },
                ]
            },
            code: '',
            uuid: ''
        }
    },
    mounted() {
        this.getCheckCode()
    },
    methods: {
        login() {
            console.log("当前res==>", this.user)
            this.$refs['userForm'].validate((valid) => {
                // 验证表单内容是否符合规则
                if (valid) {
                    this.user.codeUUID = this.uuid
                    this.request.post("/user/login", this.user).then(res => {
                        if (res.code == 200) {
                            localStorage.setItem("user", JSON.stringify(res.data))  // 存储用户信息到浏览器，通过JSON.stringify转换成正确的json数据
                            localStorage.setItem("menus", JSON.stringify(res.data.menus))  // 存储用户信息到浏览器
                            // 动态设置当前用户的路由
                            setRoutes()
                            this.$message.success("登录成功")
                            //登录成功之后跳转主页面
                            this.$router.push("/")
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                }
            });
        },
        jumpRegister() {
            this.$router.push("/register")
        },
        getCheckCode() {
            this.request.get("/common/getCheckCode").then(res => {
                this.code = res.data.img
                this.uuid = res.data.imgUUID
            })
        }
    }
}
</script>

<style>
.wrapper {
    height: 100vh;

    background-image: url(../assets/login.jpg);
    background-size: 100% 100%;
    overflow: hidden;
    /* opacity: 0.8; */
}

@keyframes gradientBG {
    0% {
        background-position: 0 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0 50%;
    }
}
</style>